package com.tank.collision;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.EffectComponent;
import com.almasb.fxgl.dsl.components.HealthIntComponent;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.physics.CollisionHandler;
import com.tank.TankApp;
import com.tank.components.BulletLevelComponent;
import com.tank.constant.GameType;
import com.tank.constant.ItemType;
import com.tank.effect.FreezeEffect;
import com.tank.effect.HelmetEffect;
import com.tank.effect.ShipEffect;

import java.util.List;

public class ItemPlayerCollideHandler extends CollisionHandler {
    public ItemPlayerCollideHandler() {
        super(GameType.ITEM, GameType.PLAYER);
    }

    @Override
    protected void onCollision(Entity item, Entity player) {
        ItemType itemType = item.getObject("itemType");
        switch (itemType) {
            case GUN -> player.getComponent(BulletLevelComponent.class).fullGrade();
            case STAR -> player.getComponent(BulletLevelComponent.class).upgrade();
            case TANK -> player.getComponent(HealthIntComponent.class).restore(1);
            case HEART -> player.getComponent(HealthIntComponent.class).restoreFully();
            case BOMB -> FXGL.getGameWorld().getEntitiesByType(GameType.ENEMY).forEach(Entity::removeFromWorld);
            case SHIP -> player.getComponent(EffectComponent.class).startEffect(new ShipEffect());
            case TIME -> {
                List<Entity> enemies = FXGL.getGameWorld().getEntitiesByType(GameType.ENEMY);
                for (Entity enemy : enemies) {
                    enemy.getComponent(EffectComponent.class).startEffect(new FreezeEffect());
                }
            }
            case HELMET -> player.getComponent(EffectComponent.class).startEffect(new HelmetEffect());
            case SPADE -> FXGL.<TankApp>getAppCast().reinforce();
        }
        item.removeFromWorld();
        FXGL.play("item.wav");
    }
}
